"""
API异常类
"""
from flask import request, json
from  werkzeug.exceptions import HTTPException

class APIException(HTTPException):
    """
    400 请求参数错误
    401 未授权
    403 禁止访问
    404 资源未找到
    200 成功
    201 更新成功
    204 删除成功
    301 重定向
    """
    code = 500
    msg=''
    error_code=999

    def __init__(self,msg=None,code=None,error_code=None,headers=None):
        if code:
            self.code=code
        if error_code:
            self.error_code=error_code
        if msg:
            self.msg=msg
        super(APIException,self).__init__(msg,None)

    def get_body(self, environ=None):
        body=dict(
            msg=self.msg,
            error_code=self.error_code,
            request=request.method+''+self.get_url_no_param()
        )
        text=json.dumps(body)
        return text

    def get_headers(self, environ=None):
        return [('Content-Type','application/json')]
    @staticmethod
    def get_url_no_param():
        full_path=str(request.full_path)
        main_path=full_path.split('?')
        return main_path[0]
